import type { RouteLocationRaw, Router } from "vue-router";

import { PageEnum } from "@/enums/PageEnum";
import { unref } from "vue";

import { useRouter } from "vue-router";
export type RouteLocationRawEx =
  | (Omit<RouteLocationRaw, "path"> & { path: PageEnum })
  | RouteLocationRaw;
function handleError(e: Error) {
  console.error(e);
}

export function useGo(_router?: Router) {
  let router;
  if (!_router) {
    router = useRouter();
  }
  // @ts-ignore
  const { push, replace } = _router || router;
  function go(opt: PageEnum | RouteLocationRawEx | string, isReplace = false) {
    if (!opt) {
      return;
    }
    if (typeof opt === "string") {
      isReplace
        ? replace(opt).catch(handleError)
        : push(opt).catch(handleError);
    } else {
      const o = opt as RouteLocationRaw;
      isReplace ? replace(o).catch(handleError) : push(o).catch(handleError);
    }
  }
  return go;
}
